package com.yoosora.utils;

import com.yoosora.common.mode.Operators;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.lang.reflect.Field;
import java.util.Date;

public class DOUtils {

	public DOUtils() {
	}

	public static void setDefaultInsertFields(Object o, Operators operators) {
		String operatorId = operators != null ? operators.getOperatorId() : null;
		String operator = operators != null ? operators.getOperator() : null;
		String prodId = operators != null ? operators.getOptSystem() : null;
		setDefaultInsertFields(o, operatorId, operator, prodId);
	}

	public static void setDefaultUpdateFields(Object o, Operators operators) {
		String operatorId = operators != null ? operators.getOperatorId() : null;
		String operator = operators != null ? operators.getOperator() : null;
		String prodId = operators != null ? operators.getOptSystem() : null;
		setDefaultUpdateFields(o, operatorId, operator, prodId);
	}

	public static void setDefaultInsertFields(Object o, String operatorId, String operator, String prodId) {
		Date now = new Date();
		Class clz = o.getClass();

		try {
			Field deleted = FieldUtils.getField(clz, "deleted", Boolean.TRUE);
			if (deleted != null) {
				deleted.set(o, Boolean.FALSE);
			}

			Field createdDate = FieldUtils.getField(clz, "createdDate", Boolean.TRUE);
			if (createdDate != null) {
				createdDate.set(o, now);
			}

			Field lastModifiedDate = FieldUtils.getField(clz, "lastModifiedDate", Boolean.TRUE);
			if (lastModifiedDate != null) {
				lastModifiedDate.set(o, now);
			}

			Field creProId;
			Field modProId;
			if (Strings.isNotBlank(operatorId)) {
				creProId = FieldUtils.getField(clz, "createdId", Boolean.TRUE);
				if (creProId != null) {
					creProId.set(o, operatorId);
				}

				modProId = FieldUtils.getField(clz, "lastModifiedId", Boolean.TRUE);
				if (modProId != null) {
					modProId.set(o, operatorId);
				}
			}

			if (Strings.isNotBlank(operator)) {
				creProId = FieldUtils.getField(clz, "createdBy", Boolean.TRUE);
				if (creProId != null) {
					creProId.set(o, operator);
				}

				modProId = FieldUtils.getField(clz, "lastModifiedBy", Boolean.TRUE);
				if (modProId != null) {
					modProId.set(o, operator);
				}
			}

			if (Strings.isNotBlank(prodId)) {
				creProId = FieldUtils.getField(clz, "creProId", Boolean.TRUE);
				if (creProId != null) {
					creProId.set(o, prodId);
				}

				modProId = FieldUtils.getField(clz, "modProId", Boolean.TRUE);
				if (modProId != null) {
					modProId.set(o, prodId);
				}
			}

		} catch (Exception var11) {
			throw new RuntimeException(var11);
		}
	}

	public static void setDefaultUpdateFields(Object o, String operatorId, String operator, String prodId) {
		Date now = new Date();
		Class clz = o.getClass();

		try {
			Field lastModifiedDate = FieldUtils.getField(clz, "lastModifiedDate", Boolean.TRUE);
			if (lastModifiedDate != null) {
				lastModifiedDate.set(o, now);
			}

			Field modProId;
			if (Strings.isNotBlank(operatorId)) {
				modProId = FieldUtils.getField(clz, "lastModifiedId", Boolean.TRUE);
				if (modProId != null) {
					modProId.set(o, operatorId);
				}
			}

			if (Strings.isNotBlank(operator)) {
				modProId = FieldUtils.getField(clz, "lastModifiedBy", Boolean.TRUE);
				if (modProId != null) {
					modProId.set(o, operator);
				}
			}

			if (Strings.isNotBlank(prodId)) {
				modProId = FieldUtils.getField(clz, "modProId", Boolean.TRUE);
				if (modProId != null) {
					modProId.set(o, prodId);
				}
			}

		} catch (Exception var8) {
			throw new RuntimeException(var8);
		}
	}
}